package com.cloud.shopping.listener;

import com.cloud.shopping.job.dynamic.DynamicJob;
import com.cloud.shopping.job.dynamic.DynamicTaskCreate;
import com.cloud.shopping.seckill.model.SeckillActivity;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

/**
 * @author John.zhang
 * @version Id: SeckillActivityHandler, v 1.0 2021/1/8 13:46 ZhiYong Exp $
 */
@Component
@CanalTable(value = "seckill_activity")
public class SeckillActivityHandler implements EntryHandler<SeckillActivity> {

    @Resource
    private DynamicTaskCreate dynamicTaskCreate;

    /****
     * 增加活动
     * @param seckillActivity seckillActivity
     */
    @Override
    public void insert(SeckillActivity seckillActivity) {
        //创建任务调度，活动结束的时候执行
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
        String cron = simpleDateFormat.format(seckillActivity.getEndTime());
        System.out.println("cron:" + cron);
        dynamicTaskCreate.create(seckillActivity.getId(), cron, 1, new DynamicJob(), seckillActivity.getId());
    }

    @Override
    public void update(SeckillActivity before, SeckillActivity after) {
        //创建任务调度，活动结束的时候执行
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
        String cron = simpleDateFormat.format(after.getEndTime());
        System.out.println("cron:" + cron);
        dynamicTaskCreate.create(after.getId(), cron, 1, new DynamicJob(), after.getId());
    }

    @Override
    public void delete(SeckillActivity seckillActivity) {
        //创建任务调度，活动结束的时候执行
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
        String cron = simpleDateFormat.format(seckillActivity.getEndTime());
        System.out.println("cron:" + cron);
        dynamicTaskCreate.create(seckillActivity.getId(), cron, 1, new DynamicJob(), seckillActivity.getId());
    }

}
